// compile: g++ -o prod_cons prod_cons.cc -std=c++11
// run: ./prod_cons
#include <chrono>
#include <condition_variable>
#include <future>
#include <iostream>
#include <mutex>
#include <queue>
using namespace std;

mutex mtx;
condition_variable cond;
queue<int> que;

void Produce() {
  int count = 0;
  while (1) {
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    unique_lock<mutex> lk(mtx);
    que.push(1);
    cout << "Producer " << ++count << endl;
    cond.notify_all();
  }
}
void Consumer() {
  int count = 0;
  while (1) {
    unique_lock<mutex> lk(mtx);
    while (!que.size()) {
      cond.wait(lk);
    }
    que.pop();
    cout << "Consumer" << ++count << endl;
  }
}

int main() {
  // 生产者
  std::thread pro1(Produce);
  std::thread pro2(Produce);
  // 消费者
  std::thread con1(Consumer);

  pro1.join();
  pro2.join();
  con1.join();

  return 0;
}